<?php

namespace app\admin\service;

use app\admin\dao\ProfessionalQaDao;
use app\admin\model\UM as UM;
use app\common\model\ProfessionalQa;
use ShidianUtil\Exception\DataNotExistException;
use ShidianUtil\Utils\TimeUtil;
use think\exception\ValidateException;

/**
 * Class ProfessionalQaService
 * @package app\professionalqa\service
 */
class ProfessionalQaService
{
    private ProfessionalQaDao $professionalQaDao;

    public function __construct(ProfessionalQaDao $professionalQaDao)
    {
        $this->professionalQaDao = $professionalQaDao;
    }

    /**
     * @param \app\admin\model\UM|null $user
     * @param $categoryId
     * @param $startTime
     * @param $endTime
     * @param $kw
     * @param int|null $page
     * @param int|null $page_size
     * @return array
     */
    public function index(?UM $user, $isEnd, $categoryId, $startTime, $endTime, $kw, ?int $page = null, ?int $page_size = null): array
    {
        if(is_numeric($endTime)){
            $endTime = TimeUtil::stamp(TimeUtil::DAY, $endTime, false);
        }
        return $this->professionalQaDao->listProfessionalQas($isEnd, $categoryId, $startTime, $endTime, $kw, $page, $page_size);
    }

    /**
     * 查询
     * @param \app\admin\model\UM|null $user
     * @param int $id
     * @return \app\common\model\ProfessionalQa
     */
    public function read(?UM $user, int $id): ?ProfessionalQa
    {
        return $this->professionalQaDao->findProfessionalQa($id);
    }

    /**
     * 新增
     * @param \app\admin\model\UM $user
     * @param array $params
     * @return  ProfessionalQa
     */
    public function add(UM $user, array $params): ProfessionalQa
    {
        $params['userId'] = $user->id;
        $params['checkStatus'] = 1;
        $params['checkTime'] = time();
        $params['isEnd'] = 0;
        $params['has_answer'] = 0;

        $this->checkRepeat($params['question']);

        return $this->professionalQaDao->insertActive($params, []);
    }

    /**
     * 更新
     * @param \app\admin\model\UM $user
     * @param int $id
     * @param array $params
     * @return \app\common\model\ProfessionalQa
     */
    public function modify(UM $user, int $id, array $params): ProfessionalQa
    {
        //这里如果要修改查询，建议把查询就放到这里
        $record = ProfessionalQa::field(true)->where('id', $id)->find();

        if (is_null($record)) {
            throw new DataNotExistException('该对象不存在或已被删除');
        }

        $this->checkRepeat($params['question'], $id);

        $allowField = ['question', 'categoryId'];
        $this->professionalQaDao->updateActive($record, $params, $allowField);

        return $record;
    }

    /**
     * 删除
     * @param \app\admin\model\UM $user
     * @param int $id
     */
    public function rm(UM $user, int $id): void
    {
        //这里如果要修改查询，建议把查询就放到这里
        $record = ProfessionalQa::field(true)->where('id', $id)->find();

        if (is_null($record)) {
            return;
        }

        $this->professionalQaDao->deleteModel($record);
    }

    private function checkRepeat($name, $id = null)
    {

        $record = ProfessionalQa::where('question', $name)
            ->when(is_id($id), function ($query) use ($id) {
                $query->where('id', '<>', $id);
            })
            ->find();

        if (!is_null($record)) {
            throw new ValidateException('该分类已存在');
        }
    }
}
